<?php
	session_start();
	$connect = mysqli_connect('localhost', 'root', 'root', 'pr8.3');
	$NoE = mysqli_query($connect, "SELECT COUNT(*) FROM `questions`");
	$questions = mysqli_query($connect, "SELECT * FROM `questions` ORDER BY RAND() LIMIT 10");
?>

<!DOCTYPE html>
<html lang="ru">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Викторина по БД</title>
	<link rel="stylesheet" href="../css/style.css">
	<link rel="shortcut icon" href="https://bsquare.co.in/assets/php_icon.png">
</head>
<body>
	<div class="content-wraper">
		<div class="content">
			<h1 class="title">Викторина по БД</h1>
			<form action="form.php" method="GET">
				<?php 
					$d = array();
					for ($qs = []; $row = mysqli_fetch_assoc($questions); $qs[] = $row);
					for ($i = 0; $i < count($qs); $i++){
						$questionID = $qs[$i]["id"];
						echo "<span>".$qs[$i]['question']."</span><br>";
						$answers = mysqli_query($connect, "SELECT * FROM `answers` WHERE `id_question` = ".$questionID);
						for ($ans = []; $row = mysqli_fetch_assoc($answers); $ans[] = $row);
						for ($j = 0; $j < count($ans); $j++){

							$answerID = $ans[$j]['id'];
							
							echo '<input type="radio" name="q'.$questionID.'" id="q'.$answerID.'" value="'.$ans[$j]['id'].'">';
							echo '<label for="q'.$answerID.'"> '.$ans[$j]['answer'].'</label><br>';
							if ($ans[$j]['type'] == 1){
								$d[$questionID] = $answerID;
							}
						}
						echo "<br>";
					}
					$_SESSION["decision"] = $d;
					$_SESSION["NoE"] = count($qs);
				?>
				<button type="submit">Отправить</button>
				<a href="/"><button type="button">На главную</button></a>
			</form>
		</div>
	</div>
</body>
</html>
